<template>
  <div class="login-container">
    <div class="login-card">
      <div class="login-header">
        <img src="../../assets/3cb49ce7-afcf-451a-8535-d106e36f11b3.png" alt="Logo" class="logo" />
        <h2>考试系统管理后台</h2>
        <p>请登录您的账号以继续</p>
      </div>
      <form class="login-form" @submit.prevent="handleLogin">
        <div class="form-group">
          <label for="username">
            <i class="fas fa-user"></i>
            <span>用户名</span>
          </label>
          <input
            type="text"
            id="username"
            v-model="username"
            placeholder="请输入用户名"
            required
            autocomplete="username"
          >
        </div>
        <div class="form-group">
          <label for="password">
            <i class="fas fa-lock"></i>
            <span>密码</span>
          </label>
          <div class="password-input">
            <input
              :type="showPassword ? 'text' : 'password'"
              id="password"
              v-model="password"
              placeholder="请输入密码"
              required
              autocomplete="current-password"
            >
            <button
              type="button"
              class="toggle-password"
              @click="togglePasswordVisibility"
              aria-label="切换密码可见性"
            >
              <i :class="showPassword ? 'fas fa-eye-slash' : 'fas fa-eye'"></i>
            </button>
          </div>
        </div>
        <div class="form-options">
          <label class="remember-me">
            <input type="checkbox" v-model="rememberMe">
            <span>记住我</span>
          </label>
          <a href="#" class="forgot-password">忘记密码?</a>
        </div>
        <button type="submit" class="login-button" :disabled="isLoading">
          <span v-if="!isLoading">登录</span>
          <i v-else class="fas fa-spinner fa-spin"></i>
        </button>
      </form>
      <div class="login-footer">
        <p>没有账号? <a href="#">联系管理员</a></p>
      </div>
      <div class="theme-selector-container">
        <div class="theme-buttons">
          <button
            v-for="theme in themeStore.systemThemes"
            :key="theme.id"
            class="theme-button"
            :class="{ active: themeStore.currentThemeId === theme.id }"
            @click="setTheme(theme.id)"
            :title="theme.name"
          >
            <i :class="theme.icon"></i>
          </button>
        </div>

        <!-- 自动主题切换 -->
        <div class="auto-theme-toggle">
          <label class="toggle-switch">
            <input
              type="checkbox"
              v-model="autoThemeEnabled"
              @change="toggleAutoTheme"
            >
            <span class="toggle-slider"></span>
          </label>
          <span class="auto-theme-label">跟随系统</span>
        </div>
      </div>
    </div>
    <Loading
      :size="60"
      color="var(--primary)"
      shadowColor="rgba(0, 0, 0, 0.5)"
      :speed="0.6"
      :loading="isLoading"
      backgroundColor="rgba(0, 0, 0, 0.5)">
</Loading>
  </div>
</template>


<script setup>
import { ref, computed, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { UserService } from '@/api/UserService';
import { useUserStore } from '../../stores/useStore';
import { ElMessage } from 'element-plus'
import { useThemeStore } from '@/stores/modules/theme';
import Loading from '@/components/commont/Loading.vue';
const useStore=useUserStore()
const username = ref('');
const password = ref('');
const router = useRouter();
const rememberMe = ref(false);
const showPassword = ref(false);
const isLoading = ref(false);
const themeStore = useThemeStore();
// 自动主题模式
const autoThemeEnabled = ref(themeStore.autoThemeMode);


const togglePasswordVisibility = () => {
  showPassword.value = !showPassword.value;
};

const setTheme = (themeId) => {
  themeStore.setTheme(themeId);
  // 如果启用了自动主题，则禁用它
  if (themeStore.autoThemeMode) {
    themeStore.setAutoThemeMode(false);
    autoThemeEnabled.value = false;
  }
};
const toggleAutoTheme = () => {
  themeStore.setAutoThemeMode(autoThemeEnabled.value);
};

const handleLogin =async () => {
  // Here you would typically make an API call to verify credentials
  isLoading.value=true;
  console.log(username,password)
 const response= await  UserService.login(
  {userName:username.value
  ,password:password.value});

  if(response.code==1){
    useStore.setUserAndToken(response.data.user,response.data.token)
    console.log(response)
    console.log('Login attempt:', username.value, password.value);
  // For demo purposes, we'll just redirect to the dashboard
  router.push('/dashboard');
  isLoading.value=false;
  }
else{
  ElMessage({
        message: '失败',
        type: 'error',
      });
      isLoading.value=false;
}
};
// 如果之前选择了"记住我"，自动填充用户名
onMounted(() => {
  const rememberedUsername = localStorage.getItem('rememberedUsername');
  if (rememberedUsername) {
    username.value = rememberedUsername;
    rememberMe.value = true;
  }
  // 初始化主题状态
  themeStore.initTheme();
});
</script>

<style scoped>


.login-container {
  display: flex;
  justify-content: center;
  align-items: center;
  min-height: 100vh;
  background: linear-gradient(
    135deg,
    var(--primary) 0%,
    var(--highlight) 50%,
    var(--primary-dark) 100%
  );
  background-size: 400% 400%;
  animation: gradientFlow 12s ease infinite;
  padding: 2rem;
  position: relative;
  overflow: hidden;
}

/* 添加动态粒子背景效果 */
.login-container::before {
  content: '';
  position: absolute;
  width: 200%;
  height: 200%;
  background: radial-gradient(circle,
    rgba(255,255,255,0.1) 10%,
    transparent 20%);
  animation: particleMove 20s linear infinite;
}

@keyframes gradientFlow {
  0% { background-position: 0% 50%; }
  50% { background-position: 100% 50%; }
  100% { background-position: 0% 50%; }
}

@keyframes particleMove {
  0% { transform: translate(-50%, -50%) rotate(0deg); }
  100% { transform: translate(-50%, -50%) rotate(360deg); }
}



.login-card {
  background-color: var(--component-background);
  border-radius: 10px;
  box-shadow: 0 10px 25px var(--shadow-color);
  width: 100%;
  max-width: 600px;
  overflow: hidden;
  position: relative;
}

.login-header {
  text-align: center;
  padding: 2rem 1.5rem;
  background-color: rgba(0, 0, 0, 0.02);
}

.dark .login-header {
  background-color: rgba(255, 255, 255, 0.02);
}

.logo {
  width: 80px;
  height: 80px;
  margin-bottom: 1rem;
}

.login-header h2 {
  color: var(--text);
  margin: 0 0 0.5rem;
  font-size: 1.5rem;
}

.login-header p {
  color: var(--text-secondary);
  margin: 0;
}

.login-form {
  padding: 1.5rem;
}

.form-group {
  margin-bottom: 1.5rem;
}

.form-group label {
  display: flex;
  align-items: center;
  margin-bottom: 0.5rem;
  color: var(--text);
  font-weight: 500;
}

.form-group label i {
  margin-right: 0.5rem;
  color: var(--primary);
}

.form-group input {
  width: 96%;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  font-size: 1rem;
  transition: border-color var(--transition-duration);
  background-color: var(--component-background);
  color: var(--text);
}

.form-group input:focus {
  border-color: var(--primary);
  outline: none;
  box-shadow: 0 0 0 3px rgba(24, 144, 255, 0.2);
}

.password-input {
  position: relative;
}

.toggle-password {
  position: absolute;
  right: 0.75rem;
  top: 50%;
  transform: translateY(-50%);
  background: none;
  border: none;
  color: var(--text-secondary);
  cursor: pointer;
  transition: color var(--transition-duration);
}

.toggle-password:hover {
  color: var(--primary);
}

.form-options {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 1.5rem;
}

.remember-me {
  display: flex;
  align-items: center;
  cursor: pointer;
  color: var(--text);
}

.remember-me input {
  margin-right: 0.5rem;
}

.forgot-password {
  color: var(--text);
  text-decoration: none;
  transition: color var(--transition-duration);
}

.forgot-password:hover {
  color: var(--primary-dark);
  text-decoration: underline;
}

.login-button {
  width: 100%;
  padding: 0.75rem;
  background-color: var(--primary);
  color:  var(--text);
  border: 1px solid var(--border-color);
  border-radius: 4px;
  font-size: 1rem;
  font-weight: 500;
  cursor: pointer;

  transition: background-color var(--transition-duration);
}

.login-button:hover:not(:disabled) {
  background-color: var(--hover-color);
}

.login-button:disabled {
  background-color: #a0a0a0;
  cursor: not-allowed;
}

.login-footer {
  text-align: center;
  padding: 1rem;
  border-top: 1px solid var(--border);
  color: var(--text-secondary);
}

.login-footer a {
  color: var(--text);
  text-decoration: none;
  transition: color var(--transition-duration);
}

.login-footer a:hover {
  color: var(--primary-dark);
  text-decoration: underline;
}

.theme-selector-container {
  position: absolute;
  top: 1rem;
  right: 1rem;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 0.5rem;
}

.theme-buttons {
  display: flex;
  gap: 0.5rem;
}

.theme-button {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  background-color: transparent;
  border: 1px solid var(--border);
  cursor: pointer;
  color: var(--text-secondary);
  transition: all var(--transition-duration);
}

.theme-button:hover {
  background-color: rgba(0, 0, 0, 0.05);
  color: var(--primary);
}

.dark .theme-button:hover {
  background-color: rgba(255, 255, 255, 0.1);
}

.theme-button.active {
  color: var(--primary);
  border-color: var(--primary);
  background-color: rgba(0, 0, 0, 0.05);
}

.dark .theme-button.active {
  background-color: rgba(255, 255, 255, 0.1);
}

.auto-theme-toggle {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.auto-theme-label {
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.toggle-switch {
  position: relative;
  display: inline-block;
  width: 36px;
  height: 18px;
}

.toggle-switch input {
  opacity: 0;
  width: 0;
  height: 0;
}

.toggle-slider {
  position: absolute;
  cursor: pointer;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  background-color: #ccc;
  transition: .4s;
  border-radius: 34px;
}

.toggle-slider:before {
  position: absolute;
  content: "";
  height: 14px;
  width: 14px;
  left: 2px;
  bottom: 2px;
  background-color: white;
  transition: .4s;
  border-radius: 50%;
}

input:checked + .toggle-slider {
  background-color: var(--primary);
}

input:checked + .toggle-slider:before {
  transform: translateX(18px);
}

@media (max-width: 480px) {
  .login-card {
    border-radius: 0;
  }

  .login-header {
    padding: 1.5rem 1rem;
  }

  .login-form {
    padding: 1rem;
  }

  .theme-selector-container {
    top: 0.5rem;
    right: 0.5rem;
  }

  .theme-button {
    width: 28px;
    height: 28px;
  }
}
</style>



